/*     */ package com.zimbra.cs.gal;
/*     */ 
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.ElementFactory;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.GalContact;
/*     */ import com.zimbra.cs.account.GalContact.Visitor;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.SearchGalResult;
/*     */ import com.zimbra.cs.account.gal.GalOp;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.service.mail.ToXML;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.account.type.MemberOfSelector;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalSearchResultCallback
/*     */   implements GalContact.Visitor
/*     */ {
/*     */   private Element mResponse;
/*     */   protected ItemIdFormatter mFormatter;
/*     */   private boolean mIdOnly;
/*     */   private final GalOp mOp;
/*     */   private Account mAuthAcct;
/*     */   private boolean mNeedCanExpand;
/*     */   private boolean mNeedIsOwner;
/*     */   private MemberOfSelector mNeedIsMember;
/*     */   private boolean mNeedSMIMECerts;
/*  55 */   private Set<String> mOwnerOfGroupsIds = null;
/*     */   
/*     */   public GalSearchResultCallback(GalSearchParams params) {
/*  58 */     reset(params);
/*  59 */     this.mOp = params.getOp();
/*     */   }
/*     */   
/*     */   public void reset(GalSearchParams params) {
/*  63 */     if (params.getSoapContext() != null) {
/*  64 */       this.mResponse = params.getSoapContext().createElement(params.getResponseName());
/*  65 */       this.mFormatter = new ItemIdFormatter(params.getSoapContext());
/*     */     } else {
/*  67 */       this.mResponse = Element.XMLElement.mFactory.createElement(params.getResponseName());
/*  68 */       this.mFormatter = new ItemIdFormatter();
/*     */     }
/*  70 */     params.setGalResult(Provisioning.SearchGalResult.newSearchGalResult(this));
/*  71 */     this.mIdOnly = params.isIdOnly();
/*     */     try
/*     */     {
/*  74 */       this.mAuthAcct = params.getAuthAccount();
/*     */     } catch (ServiceException e) {
/*  76 */       ZimbraLog.gal.warn("unable to get authed account", e);
/*     */     }
/*  78 */     this.mNeedCanExpand = params.getNeedCanExpand();
/*  79 */     this.mNeedIsOwner = params.getNeedIsOwner();
/*  80 */     this.mNeedIsMember = params.getNeedIsMember();
/*  81 */     this.mNeedSMIMECerts = params.getNeedSMIMECerts();
/*     */   }
/*     */   
/*     */   public void visit(GalContact c) throws ServiceException
/*     */   {
/*  86 */     handleContact(c);
/*     */   }
/*     */   
/*     */   public Element getResponse() {
/*  90 */     return this.mResponse;
/*     */   }
/*     */   
/*     */   public boolean passThruProxiedGalAcctResponse() {
/*  94 */     return false;
/*     */   }
/*     */   
/*     */   public void handleProxiedResponse(Element resp) {
/*  98 */     if (!$assertionsDisabled) throw new AssertionError();
/*     */   }
/*     */   
/*     */   public Element handleContact(Contact c) throws ServiceException { Element eContact;
/*     */     Element eContact;
/* 103 */     if (this.mIdOnly) {
/* 104 */       eContact = this.mResponse.addNonUniqueElement("cn").addAttribute("id", this.mFormatter.formatItemId(c)); } else { Element eContact;
/* 105 */       if (this.mOp == GalOp.sync) {
/* 106 */         eContact = ToXML.encodeContact(this.mResponse, this.mFormatter, null, c, true, c.getAllFields().keySet()); } else { Element eContact;
/* 107 */         if (this.mNeedSMIMECerts)
/*     */         {
/*     */ 
/* 110 */           Set<String> fieldSet = new HashSet(c.getFields().keySet());
/* 111 */           fieldSet.addAll(Contact.getSMIMECertFields());
/* 112 */           eContact = ToXML.encodeContact(this.mResponse, this.mFormatter, null, c, true, fieldSet);
/*     */         } else {
/* 114 */           Set<String> fieldSet = new HashSet(c.getFields().keySet());
/* 115 */           fieldSet.removeAll(Contact.getSMIMECertFields());
/* 116 */           eContact = ToXML.encodeContact(this.mResponse, this.mFormatter, null, c, true, fieldSet);
/*     */         }
/*     */       } }
/* 119 */     eContact.addAttribute("ref", c.get(ContactConstants.A_dn));
/*     */     
/* 121 */     if (c.isGroup()) {
/* 122 */       String zimbraId = c.get(ContactConstants.A_zimbraId);
/*     */       
/* 124 */       if (this.mNeedCanExpand) {
/* 125 */         boolean canExpand = GalSearchControl.canExpandGalGroup(c.get(ContactConstants.A_email), zimbraId, this.mAuthAcct);
/*     */         
/* 127 */         eContact.addAttribute("exp", canExpand);
/*     */       }
/*     */       
/* 130 */       if (this.mNeedIsOwner) {
/* 131 */         boolean isOwner = isOwner(zimbraId);
/* 132 */         eContact.addAttribute("isOwner", isOwner);
/*     */       }
/*     */       
/* 135 */       if (MemberOfSelector.none != this.mNeedIsMember) {
/* 136 */         boolean isMember = isMember(zimbraId);
/* 137 */         eContact.addAttribute("isMember", isMember);
/*     */       }
/*     */     }
/*     */     
/* 141 */     return eContact;
/*     */   }
/*     */   
/*     */   public void handleContact(GalContact c) throws ServiceException {
/* 145 */     Element eGalContact = ToXML.encodeGalContact(this.mResponse, c);
/* 146 */     eGalContact.addAttribute("ref", c.getId());
/*     */     
/* 148 */     if (c.isGroup()) {
/* 149 */       String zimbraId = c.getSingleAttr(ContactConstants.A_zimbraId);
/*     */       
/* 151 */       if (this.mNeedCanExpand) {
/* 152 */         boolean canExpand = GalSearchControl.canExpandGalGroup(c.getSingleAttr(ContactConstants.A_email), zimbraId, this.mAuthAcct);
/*     */         
/* 154 */         eGalContact.addAttribute("exp", canExpand);
/*     */       }
/*     */       
/* 157 */       if (this.mNeedIsOwner) {
/* 158 */         boolean isOwner = isOwner(zimbraId);
/* 159 */         eGalContact.addAttribute("isOwner", isOwner);
/*     */       }
/*     */       
/* 162 */       if (MemberOfSelector.none != this.mNeedIsMember) {
/* 163 */         boolean isMember = isMember(zimbraId);
/* 164 */         eGalContact.addAttribute("isMember", isMember);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void handleElement(Element e) throws ServiceException {
/* 170 */     this.mResponse.addNonUniqueElement(e.detach());
/*     */   }
/*     */   
/*     */   public void handleDeleted(ItemId id) {
/* 174 */     this.mResponse.addNonUniqueElement("deleted").addAttribute("id", id.toString());
/*     */   }
/*     */   
/*     */   protected HashMap<String, Object> parseContactElement(Element e) {
/* 178 */     HashMap<String, Object> map = new HashMap();
/* 179 */     Iterator<Element> iter = e.elementIterator("a");
/* 180 */     while (iter.hasNext()) {
/* 181 */       Element elem = (Element)iter.next();
/* 182 */       String key = elem.getAttribute("n", null);
/* 183 */       String value = elem.getText();
/* 184 */       if (key != null)
/*     */       {
/* 186 */         Object obj = map.get(key);
/* 187 */         if (obj != null) {
/* 188 */           if ((obj instanceof String)) {
/* 189 */             String[] str = new String[2];
/* 190 */             str[0] = ((String)obj);
/* 191 */             str[1] = value;
/* 192 */             map.put(key, str);
/* 193 */           } else if ((obj instanceof String[])) {
/* 194 */             ArrayList<String> arr = new ArrayList();
/* 195 */             arr.addAll(Arrays.asList((String[])obj));
/* 196 */             arr.add(value);
/* 197 */             map.put(key, arr.toArray(new String[0]));
/*     */           }
/*     */         } else
/* 200 */           map.put(key, value);
/*     */       }
/*     */     }
/* 203 */     return map;
/*     */   }
/*     */   
/* 206 */   public void setNewToken(String newToken) { setNewToken(new GalSyncToken(newToken)); }
/*     */   
/*     */   public void setNewToken(GalSyncToken newToken) {
/* 209 */     String oldToken = this.mResponse.getAttribute("token", null);
/* 210 */     if (oldToken != null)
/* 211 */       newToken.merge(new GalSyncToken(oldToken));
/* 212 */     this.mResponse.addAttribute("token", newToken.toString());
/*     */   }
/*     */   
/* 215 */   public void setSortBy(String sortBy) { this.mResponse.addAttribute("sortBy", sortBy); }
/*     */   
/*     */   public void setQueryOffset(int offset) {
/* 218 */     this.mResponse.addAttribute("offset", offset);
/*     */   }
/*     */   
/* 221 */   public void setHasMoreResult(boolean more) { this.mResponse.addAttribute("more", more); }
/*     */   
/*     */   public void setThrottled(boolean throttled)
/*     */   {
/* 225 */     this.mResponse.addAttribute("throttled", throttled);
/*     */   }
/*     */   
/*     */   public void setFullSyncRecommended(boolean value) {
/* 229 */     this.mResponse.addAttribute("fullSyncRecommended", value);
/*     */   }
/*     */   
/*     */   public void setGalDefinitionLastModified(String timestamp) {
/* 233 */     this.mResponse.addAttribute("galDefinitionLastModified", timestamp);
/*     */   }
/*     */   
/*     */   private boolean isOwner(String groupZimbraId) throws ServiceException {
/* 237 */     if ((this.mAuthAcct == null) || (groupZimbraId == null)) {
/* 238 */       return false;
/*     */     }
/*     */     
/* 241 */     if (this.mOwnerOfGroupsIds == null) {
/* 242 */       this.mOwnerOfGroupsIds = Group.GroupOwner.getOwnedGroupsIds(this.mAuthAcct);
/*     */     }
/*     */     
/* 245 */     return this.mOwnerOfGroupsIds.contains(groupZimbraId);
/*     */   }
/*     */   
/*     */   private boolean isMember(String groupZimbraId) throws ServiceException {
/* 249 */     if ((this.mAuthAcct == null) || (groupZimbraId == null)) {
/* 250 */       return false;
/*     */     }
/* 252 */     return Provisioning.getInstance().inACLGroup(this.mAuthAcct, groupZimbraId);
/*     */   }
/*     */   
/*     */   public static abstract class PassThruGalSearchResultCallback extends GalSearchResultCallback {
/*     */     protected Element mProxiedResponse;
/*     */     protected boolean mPagingSupported;
/*     */     
/*     */     public PassThruGalSearchResultCallback(GalSearchParams params) {
/* 260 */       super();
/*     */     }
/*     */     
/*     */     public boolean passThruProxiedGalAcctResponse()
/*     */     {
/* 265 */       return true;
/*     */     }
/*     */     
/*     */     public void handleProxiedResponse(Element resp)
/*     */     {
/* 270 */       this.mProxiedResponse = resp;
/* 271 */       this.mProxiedResponse.detach();
/*     */     }
/*     */     
/*     */     public Element getResponse()
/*     */     {
/* 276 */       if (this.mProxiedResponse != null) {
/* 277 */         return this.mProxiedResponse;
/*     */       }
/* 279 */       Element resp = super.getResponse();
/* 280 */       resp.addAttribute("paginationSupported", this.mPagingSupported);
/* 281 */       return super.getResponse();
/*     */     }
/*     */     
/*     */ 
/*     */     public void handleElement(Element e)
/*     */       throws ServiceException
/*     */     {
/* 288 */       throw ServiceException.FAILURE("internal error, method should not be called", null);
/*     */     }
/*     */     
/*     */     public void setQueryOffset(int offset)
/*     */     {
/* 293 */       super.setQueryOffset(offset);
/* 294 */       this.mPagingSupported = true;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/gal/GalSearchResultCallback.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */